package com.naton.mydemo.ui.act

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.naton.common.callback.OnSingleClickListener
import com.naton.common.ui.BaseVMActivity
import com.naton.mydemo.bean.ChatMessage
import com.naton.mydemo.databinding.ActivityLoginBinding
import com.naton.mydemo.model.dao.AppDataBase
import com.naton.mydemo.model.dao.ChatMessageDao
import com.naton.mydemo.viewmodel.LoginViewModel

/**
 * 登录页面
 */
class LoginActivity : BaseVMActivity<ActivityLoginBinding , LoginViewModel>(){



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
        addListener()
    }

    private fun initView(){

        mViewModel?.getLoginUserLiveData()?.observe(this ,
            {
                it.telephone = "123456"
                val chatMessage = ChatMessage()
                chatMessage.msgId = "0009"
                chatMessage.content = "ggg"
                AppDataBase.getDataBase().chatMessageDao().createTable("MyChatTable")
                AppDataBase.getDataBase().chatMessageDao().insert("MyChatTable" , chatMessage)
               val chatMessageList = AppDataBase.getDataBase().chatMessageDao().queryChatMessageById("MyChatTable" , "0009")
                //跳转到主界面
                startActivity(Intent(this@LoginActivity, MainActivity::class.java))
                finish()
            })
    }

    private fun addListener(){

        //登录按钮点击事件
        mBinding.btnLogin.setOnClickListener(object : OnSingleClickListener(){
            override fun onSingleClick(view: View?) {
                mViewModel?.login(mBinding.tvTelephone.text.trim().toString()
                    , mBinding.tvPassword.text.trim().toString())
            }
        })
    }
}